package amit.util;

import java.io.File;

public class FetchAllAds {

	public static int number_of_running_scan_threads = 0;
	
	/*Object lock = new Object();
	
	public void addThread() {
		   synchronized (lock) {
			   number_of_running_scan_threads++;
		   }
	}
	
	public void removeThread() {
		   synchronized (lock) {
			   number_of_running_scan_threads--;
		   }
	}*/
	
	public final static int NO_AREA = Integer.parseInt(
			System.getProperty("no.output.area.files",
			"12")); 
	
	public FetchAllAds() {
		// clean dir
		File outDir = new File(FileUtil.DATA_DIR);
		File[] listOfFiles = outDir.listFiles();
		for (int i = 0; i < listOfFiles.length; i++) {
			listOfFiles[i].delete();
		}
	}
	
	/**
	 * @ido TODO remove all this definitions to propery file
	 * @throws Exception
	 */
	public void runOnAllSale()  {
		try {
			System.out.println("@@ going to parse ALL tel aviv"); //$NON-NLS-1$
			Runner runnerTA = new Runner(Messages.getString("FetchAllAds.sale.ta.url"), //$NON-NLS-1$
				Messages.getString("FetchAllAds.sale.ta.fielname")); //$NON-NLS-1$
			runnerTA.setRentMode(false);
			runnerTA.runOnQuery();
			Runner runnerTABait = new Runner(Messages.getString("FetchAllAds.sale.ta.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.ta.fielname")); //$NON-NLS-1$
			runnerTABait.setRentMode(false);
			runnerTABait.runOnQuery();
				
			System.out.println("@@ going to parse ALL Sharon area"); //$NON-NLS-1$
			Runner runner2 = new Runner(Messages.getString("FetchAllAds.sale.sharon.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.sharon.filename")); //$NON-NLS-1$
			runner2.setRentMode(false);
			runner2.runOnQuery();
			Runner runner2bait = new Runner(Messages.getString("FetchAllAds.sale.sharon.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.sharon.filename")); //$NON-NLS-1$
			runner2bait.setRentMode(false);
			runner2bait.runOnQuery();
			
			System.out.println("@@ going to parse ALL Jerusalem area"); //$NON-NLS-1$
			Runner runner3 = new Runner(Messages.getString("FetchAllAds.sale.jr.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.jr.filename")); //$NON-NLS-1$
			runner3.setRentMode(false);
			runner3.runOnQuery();
			Runner runner3bait = new Runner(Messages.getString("FetchAllAds.sale.jr.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.jr.filename")); //$NON-NLS-1$
			runner3bait.setRentMode(false);
			runner3bait.runOnQuery();
			
			System.out.println("@@ going to parse North country area"); //$NON-NLS-1$
			Runner runner4 = new Runner(Messages.getString("FetchAllAds.sale.north.url"), //$NON-NLS-1$
				Messages.getString("FetchAllAds.sale.north.filename")); //$NON-NLS-1$
			runner4.setRentMode(false);
			runner4.runOnQuery();
			Runner runner4bait = new Runner(Messages.getString("FetchAllAds.sale.north.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.north.filename")); //$NON-NLS-1$
			runner4bait.setRentMode(false);
			runner4bait.runOnQuery();
				
			System.out.println("@@ going to parse ALL Shefela area"); //$NON-NLS-1$
			Runner runner5 = new Runner(Messages.getString("FetchAllAds.sale.shefela.url"), //$NON-NLS-1$
				Messages.getString("FetchAllAds.sale.shefela.filename")); //$NON-NLS-1$
			runner5.setRentMode(false);
			runner5.runOnQuery();
			Runner runner5bait = new Runner(Messages.getString("FetchAllAds.sale.shefela.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.shefela.filename")); //$NON-NLS-1$
			runner5bait.setRentMode(false);
			runner5bait.runOnQuery();
				
			System.out.println("@@ going to parse South country area"); //$NON-NLS-1$
			Runner runner6 = new Runner(Messages.getString("FetchAllAds.sale.south.url"), //$NON-NLS-1$
				Messages.getString("FetchAllAds.sale.south.filename")); //$NON-NLS-1$
			runner6.setRentMode(false);
			runner6.runOnQuery();
			Runner runner6bait = new Runner(Messages.getString("FetchAllAds.sale.south.bait.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.sale.south.filename")); //$NON-NLS-1$
			runner6bait.setRentMode(false);
			runner6bait.runOnQuery();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @ido TODO remove all this to propery file
	 * @throws Exception
	 */
	public void runOnAllRent() {
		try {
				Runner runnerTA = new Runner(Messages.getString("FetchAllAds.rent.ta.url"), //$NON-NLS-1$
					Messages.getString("FetchAllAds.rent.ta.filename")); //$NON-NLS-1$
				runnerTA.setRentMode(true);
				System.out.println("@@ going to parse ALL tel aviv"); //$NON-NLS-1$
				runnerTA.runOnQuery();
				
				Runner runnerTABait = new Runner(Messages.getString("FetchAllAds.rent.ta.bait.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.sale.ta.fielname")); //$NON-NLS-1$
				runnerTABait.setRentMode(false);
				runnerTABait.runOnQuery();
				
				System.out.println("@@ going to parse ALL Sharon area"); //$NON-NLS-1$
				Runner runner2 = new Runner(Messages.getString("FetchAllAds.rent.sharon.url"),  //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.sharon.filename")); //$NON-NLS-1$
				runner2.setRentMode(true);
				runner2.runOnQuery();
				Runner runner2bait = new Runner(Messages.getString("FetchAllAds.rent.sharon.bait.url"),  //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.sharon.filename")); //$NON-NLS-1$
				runner2bait.setRentMode(true);
				runner2bait.runOnQuery();

				System.out.println("@@ going to parse ALL Jerusalem area"); //$NON-NLS-1$
				Runner runner3 = new Runner(Messages.getString("FetchAllAds.rent.jr.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.jr.filename")); //$NON-NLS-1$
				runner3.setRentMode(true);
				runner3.runOnQuery();
				Runner runner3bait = new Runner(Messages.getString("FetchAllAds.rent.jr.bait.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.jr.filename")); //$NON-NLS-1$
				runner3bait.setRentMode(true);
				runner3bait.runOnQuery();
				
				System.out.println("@@ going to parse North country area"); //$NON-NLS-1$
				Runner runner4 = new Runner(Messages.getString("FetchAllAds.rent.north.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.north.filename")); //$NON-NLS-1$
				runner4.setRentMode(true);
				runner4.runOnQuery();
				Runner runner4bait = new Runner(Messages.getString("FetchAllAds.rent.north.bait.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.north.filename")); //$NON-NLS-1$
				runner4bait.setRentMode(true);
				runner4bait.runOnQuery();
				
				System.out.println("@@ going to parse ALL Shefela area"); //$NON-NLS-1$
				Runner runner5 = new Runner(Messages.getString("FetchAllAds.rent.shefela.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.shefela.filename")); //$NON-NLS-1$
				runner5.setRentMode(true);
				runner5.runOnQuery();
				Runner runner5bait = new Runner(Messages.getString("FetchAllAds.rent.shefela.bait.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.shefela.filename")); //$NON-NLS-1$
				runner5bait.setRentMode(true);
				runner5bait.runOnQuery();

				System.out.println("@@ going to parse South country area"); //$NON-NLS-1$
				Runner runner6 = new Runner(Messages.getString("FetchAllAds.rent.south.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.south.filename")); //$NON-NLS-1$
				runner6.setRentMode(true);
				runner6.runOnQuery();
				Runner runner6bait = new Runner(Messages.getString("FetchAllAds.rent.south.bait.url"), //$NON-NLS-1$
						Messages.getString("FetchAllAds.rent.south.filename")); //$NON-NLS-1$
				runner6bait.setRentMode(true);
				runner6bait.runOnQuery();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * check how many files (area) are scaned and parsed so far
	 * @return
	 */
	public int getNoOfFiles() {
		File outDir = new File(FileUtil.DATA_DIR);
		return (outDir.listFiles()).length;
	}
	
	public boolean isDone() {
		if (getNoOfFiles() == NO_AREA) {
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		FetchAllAds fetcher = new FetchAllAds();
//		fetcher.runOnAllRent();
		fetcher.runOnAllSale();
	}
}
